package com.lhn.formssi.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration  //标识我是配置类
public class RedisConfig {
	@Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private int port;
    @Value("${redis.password}")
    private String password;
    @Value("${redis.timeout}")
    private int timeout;

    @Bean(name = "redisDataSource")
    public JedisPool createDataSource(){
    	JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    	JedisPool jedisPool = null;
    	//连接池最小空闲连接
    	jedisPoolConfig.setMinIdle(0);
    	//连接池最大空闲连接
        jedisPoolConfig.setMaxIdle(50);
        //设置最大连接数
        jedisPoolConfig.setMaxTotal(50);
        //最大连接等待时长
        jedisPoolConfig.setMaxWaitMillis(10000);
        if(password == null || password.equals("")){
            jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout);
        }else{
            jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
        }
		return jedisPool;
    }
}
